﻿Imports Microsoft.Win32
Public Class OpenVPNStatus
    Public Event SetDHCPServer(TunnelName As String, NewAddress As String)
    Public Event SetInterfaceID(TunnelName As String, NewID As String)
    Public Event SetIPAddress(TunnelName As String, NewAddress As String)
    Public Event SetNetmask(TunnelName As String, NewMask As String)
    Public Event SetStatus(TunnelName As String, NewStatus As OpenVPNTunMgr.Tunnel.TunnelStatus)
    Public Event SetPeer(TunnelName As String, PeerSocket As String)
    Public Sub ReadStatus()
        Try
            ' obtain HKEY_LOCAL_MACHINE 
            Dim RegRoot As RegistryKey = WinReg.HKEY_LOCAL_MACHINE

            ' obtain tunnel configuration
            Dim TunRoot As RegistryKey = RegRoot.OpenSubKey(Reg_Tunnels_Root)

            ' read config if available
            If IsNothing(TunRoot) Then
                ' no config
                Log("OpenVPNConfiguration: Registry key at [" & Reg_Tunnels_Root & "] not found, can't read status.", EventLogEntryType.Error)
            Else
                Dim TunNames() As String = TunRoot.GetSubKeyNames
                Dim TunStat As RegistryKey
                Dim TunStatVals() As String
                Dim TunDisabled As Boolean = False
                Dim PeerPort As Integer
                Dim PeerIP As String
                Dim TunnelState As OpenVPNWinSvc.Library.OpenVPNTunMgr.Tunnel.TunnelStatus

                'Log("OpenVPNConfiguration: There are [" & TunRoot.SubKeyCount & "] tunnels to configure.", EventLogEntryType.Information)
                For Each TunName As String In TunNames
                    ' read openvpn status for this tunnel from the registry
                    TunStat = TunRoot.OpenSubKey(TunName).OpenSubKey("Status")
                    TunStatVals = TunStat.GetValueNames
                    For Each TunStatVal As String In TunStatVals
                        Select Case LCase(TunStatVal)
                            Case "dhcp server"
                                RaiseEvent SetDHCPServer(TunName, TunStat.GetValue(TunStatVal).ToString)
                            Case "interface id"
                                RaiseEvent SetInterfaceID(TunName, TunStat.GetValue(TunStatVal).ToString)
                            Case "ip address"
                                RaiseEvent SetIPAddress(TunName, TunStat.GetValue(TunStatVal).ToString)
                            Case "netmask"
                                RaiseEvent SetNetmask(TunName, TunStat.GetValue(TunStatVal).ToString)
                            Case "status"
                                TunnelState = DirectCast([Enum].Parse(GetType(OpenVPNWinSvc.Library.OpenVPNTunMgr.Tunnel.TunnelStatus), TunStat.GetValue(TunStatVal).ToString), OpenVPNWinSvc.Library.OpenVPNTunMgr.Tunnel.TunnelStatus)
                                RaiseEvent SetStatus(TunName, TunnelState)
                            Case "peerport"
                                PeerPort = Integer.Parse(TunStat.GetValue(TunStatVal).ToString)
                                If (Not PeerIP = "") And (Not PeerPort = 0) Then RaiseEvent SetPeer(TunName, PeerIP & ":" & PeerPort)
                            Case "peerip"
                                PeerIP = TunStat.GetValue(TunStatVal).ToString
                                If (Not PeerIP = "") And (Not PeerPort = 0) Then RaiseEvent SetPeer(TunName, PeerIP & ":" & PeerPort)
                        End Select
                    Next
                Next
            End If

        Catch ex As Exception

            Stop

        End Try
    End Sub

    Private Sub Log(Message As String, EventType As EventLogEntryType)
        OpenVPNLogging.WLog("OpenVPNStatus: " & Message, EventType)
    End Sub
    Private Sub Dbg(Message As String)
        OpenVPNLogging.WDbg("OpenVPNStatus: " & Message)
    End Sub

End Class
